package nl.weeaboo.sh.objects;

import javax.media.opengl.GL;

import nl.weeaboo.game.gl.GLManager;
import nl.weeaboo.sh.view.anim.AnimationFrame;

public class GhostingHelper {

	private State states[];
	private float weights[];
	private int start, end;
	private int skip;
	
	private int skipCounter;
	
	public GhostingHelper(int length, int skip) {
		this.skip = skip;

		states = new State[length];
		weights = new float[length];
		for (int n = 0; n < states.length; n++) {
			states[n] = new State();
			weights[n] = 0.50f * (float)Math.log(1 + (Math.E-1.0) * (n+1) / states.length);
		}
	}
	
	//Functions
	public void reset() {
		start = end = 0;
	}
	
	public void update(int x, int y, Object image, boolean mirror) {
		if (skipCounter < skip) {
			skipCounter++;
		}
		skipCounter = 0;
		
		states[end].set(x, y, image, mirror);
		end = (end + 1) % states.length;
		if (end == start) {
			start = (start + 1) % states.length;
		}
	}
	public void draw(GLManager glm) {
		GL gl = glm.getGL();
		glm.pushColor();
		gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);
		
		int t = 0;
		int n = start;
		while (n != end) {
			glm.translate(states[n].x, states[n].y);
			glm.setColor(1, 1, 1, weights[t]);
			AnimationFrame.draw(glm, states[n].mirror, states[n].image);
			glm.translate(-states[n].x, -states[n].y);
			
			t++;
			n++;
			if (n >= states.length) n -= states.length;
		}
		
		gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
		glm.popColor();
	}
	
	//Getters
	
	//Setters
	
	
	
	//Inner Classes
	private static class State {
		
		int x, y;
		Object image;
		boolean mirror;
		
		State() {
		}
		
		void set(int x, int y, Object image, boolean mirror) {
			this.x = x;
			this.y = y;
			this.image = image;
			this.mirror = mirror;
		}
		
	}
}
